import { Common } from "UtilsCommon";
import BaseIndex from './baseIndex';

export default class Dialog extends BaseIndex {
    constructor() {
        super();
        this.initExpand("dialog");
    }

    //弹出选择视图选择数据列表，可多选或单选，弹出层之前对DataGridView进行验证是否选择行。
    //行为参数，dataGridView，弹出层视图 dialogView，选择数据绑定组件DataComponent
    selectViewDataToList(props, action) {
        if (!action.parameters) this.initSelectViewDataToListAction(props, action);
        const { pageAxis, property } = props;

        const { dataGridView, dialogView, alertMessage } = action.parameters;
        const { dataAction } = dialogView;

        let selectDataList = null; let selectRowKeys = null;
        if (property.params) {
            selectDataList = [property.params];
            selectRowKeys = [property.params[dataGridView.entity.primaryKey]];

            action.getValue = () => property.params;
        }
        else {
            selectRowKeys = dataGridView.getSelectedRowKeys();
            if (selectRowKeys.length === 0) {
                this.alert("请选择记录再操作！", pageAxis.showMessage, alertMessage);
                return;
            }

            if (selectRowKeys.length > 0) if (alertMessage) alertMessage.setValue("");

            dialogView.selectRowKeys = selectRowKeys;

            selectDataList = dataGridView.getSelectDataList();
        }

        if (!dialogView.setSelectValuesOkActionType) dialogView.setSelectValuesOkActionType = this.getDataActionType(pageAxis, dataAction);

        //设置接收数据行数返回数据
        pageAxis.receives[dialogView.setSelectValuesOkActionType] = (d) => this.receiveDialogOkActionType(d, props, action);

        //扩展数据加载
        if (dialogView.expandDataLoad) dialogView.expandDataLoad(props, action, selectDataList);

        const title = Common.replaceDataContent(selectDataList[0], dialogView.dialogTitle);

        const onOk = (e, p) => this.setSelectViewDataToList(e, p, props, action, selectRowKeys);
        this.showdialog(action, pageAxis, dialogView, onOk, action.setValue, title);
    }

    //弹出层确定事件行为
    setSelectViewDataToList(e, p, props, action, selectRowKeys) {
        const { dialogView, dataComponent, dataProperties, dataGridView } = action.parameters;
        const { pageAxis, property } = props;
        const { dataAction } = dialogView;

        action.okProperty = p;

        //获取选择值
        let selectValues = null; let selectData = null;
        if (dataComponent) {
            selectValues = dataComponent.getValue();
            if (!(selectValues && selectValues.length > 0)) {
                pageAxis.alert("请选择记录再操作！");
                return;
            }
        }
        else if (dataProperties) selectData = this.getPropertyValues(dataProperties, pageAxis);
        else selectData = this.getPropertyValues(dialogView.properties, pageAxis);

        if (selectData === false) return;

        const selectDataList = dataGridView.getSelectDataList();

        const data = { selectRowKeys, selectValues, dataAction, rowData: property.params, selectData, rowDataList: selectDataList, pageData: pageAxis.pageData };

        //禁用确定按钮
        p.setDisabled(true);

        //数据行为跟页面调用数据行为走
        //setSelectValuesOkActionType:设置选择值集合确认数据行为类型
        pageAxis.invokeDataAction(dialogView.setSelectValuesOkActionType, data);
    }

    initSelectViewDataToListAction(props, action) {
        const { pageAxis } = props;
        const dataGridView = pageAxis.getProperty(action.dataGridView);
        const dialogView = Common.arrayFirst(pageAxis.pageConfig.dialogViews, (f) => f.name === action.dialogView);

        //DataComponent存在，则取DataComponent，不存在取DataProperties属性名集合
        let dataComponent = null; let dataProperties = null;

        action.currentValue = null;
        if (action.dataComponent) {
            dataComponent = Common.arrayFirst(dialogView.properties, (f) => f.name === action.dataComponent);
            action.setValue = () => dataComponent.setValue(action.currentValue);
        }
        else {
            dataProperties = this.getSelectToList(dialogView.properties, action.dataProperties);
            action.setValue = () => {
                const value = action.getValue ? action.getValue() : action.currentValue;
                this.setPropertiesValue(dataProperties, value);
            };
        }

        const alertMessage = pageAxis.getProperty(action.alertMessage);

        action.parameters = { dataGridView, dialogView, dataComponent, dataProperties, alertMessage };
    }

    //弹出层搜索查询选择行数据
    searchQueryDataSelectRowData(props, action) {
        if (!action.parameters) this.initSearchQueryDataSelectRowDataAction(props, action);
        const { pageAxis } = props;
        const { dialogView } = action.parameters;

        const onOk = (e, p) => this.setSelectValueDialogOk(e, p, props, action);
        this.showdialog(action, pageAxis, dialogView, onOk);
    }

    setSelectValueDialogOk(e, p, props, action) {
        const { dataGridView, toSetView } = action.parameters;
        const { pageAxis } = props;

        const selectDataList = dataGridView.getSelectDataList();
        if (selectDataList.length === 0) {
            pageAxis.alert("请选择记录再操作！");
            return;
        }

        toSetView.SelectData = selectDataList[0];

        this.setViewPropertiesValue(toSetView.properties, toSetView.SelectData);

        action.modalDialog.setIsVisible(false);
    }

    initSearchQueryDataSelectRowDataAction(props, action) {
        const { pageAxis } = props;
        //设置数据视图
        const toSetView = pageAxis.getProperty(action.toSetView);
        const dialogView = Common.arrayFirst(pageAxis.pageConfig.dialogViews, (f) => f.name === action.dialogView);
        const dataGridView = pageAxis.getViewProperty(dialogView, action.dataGridView);

        action.parameters = { dataGridView, dialogView, toSetView };
    }

    //弹出层查看
    showDialogLookData(props, action) {
        if (!action.parameters) this.initShowDialogLookDataAction(props, action);
        const { pageAxis, property } = props;
        const { dialogView, lookView } = action.parameters;

        const data = property.params;
        if (data) lookView.primaryKeyValue = data[lookView.primaryKey];

        const properties = lookView.properties.filter(f => f.isClear);
        this.setPropertiesValue(properties);

        if (lookView.isReLoad && lookView.reLoad) lookView.reLoad();

        const title = Common.replaceDataContent(data, dialogView.dialogTitle);

        this.showdialog(action, pageAxis, dialogView, undefined, undefined, title);
    }

    initShowDialogLookDataAction(props, action) {
        const { pageAxis } = props;
        //设置数据视图
        const dialogView = Common.arrayFirst(pageAxis.pageConfig.dialogViews, (f) => f.name === action.dialogView);
        const lookView = pageAxis.getViewProperty(dialogView, action.lookView);

        action.parameters = { dialogView, lookView };
    }

    //弹出层查看
    showDialogLookRowData(props, action) {
        if (!action.parameters) this.initShowDialogLookRowDataAction(props, action);
        const { pageAxis, property } = props;
        const { dialogView } = action.parameters;

        let data = property.params || property.data;

        if (action.setDataNames) {
            const d = {};
            action.setDataNames.split(',').forEach(n => {
                d[n] = data[n];
            });
            data = d;
        }

        dialogView.data = data;

        this.setPropertiesValue(dialogView.properties, data, false, true);

        this.showdialog(action, pageAxis, dialogView);
    }

    initShowDialogLookRowDataAction(props, action) {
        const { pageAxis } = props;
        //设置数据视图
        const dialogView = Common.arrayFirst(pageAxis.pageConfig.dialogViews, (f) => f.name === action.dialogView);

        action.parameters = { dialogView };
    }

    //弹出层编辑
    showDialogEditEntityData(props, action) {
        if (!action.parameters) this.initShowDialogEditEntityData(props, action);
        const { pageAxis, property } = props;
        const { dialogView, dataGridView, editView } = action.parameters;

        if (action.isUpdate && !property.params) {
            const selectRowKeys = dataGridView.getSelectedRowKeys();
            if (selectRowKeys.length === 0) {
                this.alert("请选择记录再操作！", pageAxis.showMessage);
                return;
            }
            editView.primaryKeyValue = selectRowKeys[0];
        }
        else {
            editView.primaryKeyValue = null;
            editView.entityData = null;
        }

        const data = property.params ? property.params : null;

        if (data) {
            const primaryKey = editView.primaryKey || editView.entity.primaryKey;
            editView.entityData = data;
            editView.primaryKeyValue = data[primaryKey];
        }

        if (editView.isSetData && data) {
            setTimeout(() => this.setPropertiesValue(editView.properties, data), 300);
        }
        else {
            const properties = editView.properties.filter(f => f.isClear);
            this.setPropertiesValue(properties);
        }

        if (editView.isReLoad && editView.reLoad) editView.reLoad();

        const onOk = (e, p) => this.setEditEntityData(e, p, props, action);
        this.showdialog(action, pageAxis, dialogView, onOk);
    }

    setEditEntityData(e, p, props, action) {
        const { pageAxis, view } = props;
        const { dataGridView } = action.parameters;

        action.successCallback = ({ data, props, action }) => {
            action.modalDialog.setIsVisible(false);

            dataGridView && dataGridView.refresh(!!data.PrimaryKey && action.isClearQuery);
        };
        pageAxis.eventActions.entityEdit.saveEntityData({ pageAxis, view, property: p }, action);
    }

    initShowDialogEditEntityData(props, action) {
        const { pageAxis } = props;
        const dialogView = Common.arrayFirst(pageAxis.pageConfig.dialogViews, (f) => f.name === action.dialogView);

        const editView = pageAxis.getProperty(action.editView);
        const dataGridView = pageAxis.getProperty(action.dataGridView);
        const successCallback = pageAxis.getFunction(action.successCallback);
        const expandSetEntityData = pageAxis.getFunction(action.expandSetEntityData);

        action.parameters = { dialogView, dataGridView, editView, successCallback, expandSetEntityData };
    }

    //弹出层编辑
    showDialogEditComplexData(props, action) {
        if (!action.parameters) this.initShowDialogEditComplexData(props, action);
        const { pageAxis, property } = props;
        const { dialogView, editView } = action.parameters;

        if (dialogView.isSetData && property.data) {
            editView.entityData = property.data;
            setTimeout(() => this.setPropertiesValue(editView.properties, property.data, editView.isUpdateReadOnly, true), 300);
        }
        else {
            const properties = editView.properties.filter(f => f.isClear);
            this.setPropertiesValue(properties);
        }

        if (editView.isReLoad && editView.reLoad) editView.reLoad();

        const onOk = action.isEdit ? (e, p) => this.setEditComplexData(e, p, props, action) : null;
        this.showdialog(action, pageAxis, dialogView, onOk);
    }

    setEditComplexData(e, p, props, action) {
        const { pageAxis } = props;

        action.successCallback = () => {
            action.modalDialog.setIsVisible(false);
        };
        pageAxis.eventActions.complexDataGrid.saveEntityData(props, action);
    }

    initShowDialogEditComplexData(props, action) {
        const { pageAxis, } = props;
        const dialogView = Common.arrayFirst(pageAxis.pageConfig.dialogViews, (f) => f.name === action.dialogView);

        const editView = pageAxis.getViewProperty(dialogView, action.editView);
        const expandSetEntityData = pageAxis.getFunction(action.expandSetEntityData);
        const complexDataGrid = props.complexDataGrid || pageAxis.getProperty(action.complexDataGrid);

        action.parameters = { dialogView, editView, complexDataGrid, expandSetEntityData };
    }

    selectRowDataToProperty(props, action) {
        if (!action.parameters) this.initSelectRowDataToProperty(props, action);
        const { pageAxis } = props;
        const { dialogView, dataGridView, property } = action.parameters;

        const onOk = (e, p) => this.setSelectDataToPropertyDialogOk(e, p, props, action);

        const id = property.selectData ? property.selectData[dataGridView.entity.primaryKey] : null;

        this.showdialog(action, pageAxis, dialogView, onOk, () => {
            if (property.rowSelectedIds) {
                dataGridView.rowSelectedIds = property.rowSelectedIds;
            }
            else if (id) {
                dataGridView.rowSelectedIds = [id];
                property.isRefresh !== false && dataGridView.refresh && dataGridView.refresh();
            }
        });
    }

    setSelectDataToPropertyDialogOk(e, p, props, action) {
        const { pageAxis } = props;
        const { dataGridView, property } = action.parameters;

        const selectRowKeys = dataGridView.getSelectedRowKeys();
        if (selectRowKeys.length === 0) {
            this.alert("请先选择行！", pageAxis.showMessage);
            return;
        }

        property.selectData = dataGridView.getSelectDataList()[0];
        property.setValue(property.selectData[property.propertyName2 || property.name]);

        action.modalDialog.setIsVisible(false);
    }

    initSelectRowDataToProperty(props, action) {
        const { pageAxis } = props;

        const dialogView = Common.arrayFirst(pageAxis.pageConfig.dialogViews, (f) => f.name === action.dialogView);
        const dataGridView = pageAxis.getViewProperty(dialogView, action.dataGridView);
        const property = action.propertyObj || pageAxis.getProperty(action.property);

        action.parameters = { dialogView, property, dataGridView };
    }
}